package cj.web.admin.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.dao.SurveyDAO;
import cj.web.admin.dao.SurveyDepotResultDAO;
import cj.web.admin.domain.Survey;
import cj.web.admin.domain.SurveyDepotResult;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/surveyDepotResult")
public class SurveyDepotResultController extends BaseController{

	private static Logger logger = Logger.getLogger(SurveyDepotResultController.class.getName());
	
	@Autowired
	private SurveyDepotResultDAO surveyDepotResultDao;
	@Autowired
	private SurveyDAO surveyDao;
	
	@Get("show")
	public String show(Model model,SurveyDepotResult surveyDepotResult) {
	
		List<Survey> objlis = surveyDao.queryAll();
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgpcjg);
        model.add("surveylis",objlis);
		return "/views/survey/surveyDepotResultList.vm";
	}

	@Post("getList")
	public String queryList(SurveyDepotResult surveyDepotResult) {
		JSONObject jo = new JSONObject();
		List<SurveyDepotResult> arr = surveyDepotResultDao.query(surveyDepotResult);
		int size = surveyDepotResultDao.querySize(surveyDepotResult);
		jo.put("data", arr);
		jo.put("draw", surveyDepotResult.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		List<Survey> objlis = surveyDao.queryAll();
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgpcjg);
        model.add("surveylis",objlis);
		return "/views/survey/surveyDepotResultDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,SurveyDepotResult surveyDepotResult,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			surveyDepotResult.setCreateUser(returnSessionObj(request).getAccount());
			surveyDepotResult.setUpdateUser(returnSessionObj(request).getAccount());
			surveyDepotResultDao.create(surveyDepotResult);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		SurveyDepotResult surveyDepotResult =surveyDepotResultDao.queryById(id);
		
		List<Survey> objlis = surveyDao.queryAll();
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgpcjg);
        model.add("surveylis",objlis);
        model.add("obj",surveyDepotResult);
		return "/views/survey/surveyDepotResultEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,SurveyDepotResult surveyDepotResult,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			surveyDepotResult.setUpdateUser(returnSessionObj(request).getAccount());
			surveyDepotResultDao.update(surveyDepotResult);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		surveyDepotResultDao.deleteById(id);
		return "r:show";
	}
}
